package android.slc.user.ui.ud.adapter

import android.slc.adapter.SelectBox
import android.slc.appbase.ui.adapter.AppMultiItemQuickAdapter
import android.slc.extras.user.config.ConstantsUser
import android.slc.extras.user.entity.SysDept
import android.slc.extras.user.entity.UserInfo
import android.slc.user.R
import android.slc.user.ui.dept.adapter.DeptBrowserProvider
import android.slc.user.ui.user.adapter.UserInfoBrowserProvider
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.checkbox.MaterialCheckBox
import java.io.Serializable

/**
 * @author slc
 * @date 2020-08-24 16:18
 */
class DeptUserInfoMultipleAdapter(data: MutableList<Serializable>) :
    AppMultiItemQuickAdapter<Serializable, BaseViewHolder>(data),
    LoadMoreModule {
    private val deptBrowserProvider = DeptBrowserProvider()
    private val userBrowserProvider = UserInfoBrowserProvider()

    init {
        addItemType(
            ConstantsUser.Key.KEY_ADAPTER_ITEM_TYPE_DEPT,
            R.layout.user_item_dept_info_browse
        )
        addItemType(
            ConstantsUser.Key.KEY_ADAPTER_ITEM_TYPE_USER,
            R.layout.user_item_user_info_select_multiple
        )
    }

    override fun getDefItemViewType(position: Int): Int {
        val item = getItem(position)
        if (item is SysDept) {
            return ConstantsUser.Key.KEY_ADAPTER_ITEM_TYPE_DEPT
        } else if (item is SelectBox<*>) {
            return ConstantsUser.Key.KEY_ADAPTER_ITEM_TYPE_USER
        }
        return super.getDefItemViewType(position)
    }

    override fun convert(holder: BaseViewHolder, item: Serializable) {
        if (item is SysDept) {
            deptBrowserProvider.convert(holder, item)
        } else if (item is SelectBox<*>) {
            userBrowserProvider.convert(holder, item.data as UserInfo)
            val checkBox: MaterialCheckBox = holder.getView(R.id.checkbox)
            checkBox.isChecked = item.isChecked
        }
    }

}